<?php
class IndexAction extends BaseAction {
    
	
	function index() {
		$this->display();
		}
	
    function qbzp(){
	    if(isset($_SESSION['hdate'])){
				  $arr=array_slice(scandir('public/img/'.$_SESSION['hdate']),2);
				  if(count($arr)>0){
				   $this->assign(dayimgfile,$arr);
				   $this->assign(day,$_SESSION['hdate']);
				     unset($_SESSION['hdate']);   
				  }
				 }
		    
	
	   $this->display();
	}
	 function zxzp(){
	  
				  $arr=array_slice(scandir('public/img/'.date('Ymd/')),2);
				  if(count($arr)>0){
				   $this->assign(todayimgfile,$arr);
				   $this->assign(today,date('Ymd/'));
				  }
			
		    
	 //   $this->assign(todayimgfile,array_slice(scandir('public/img/'.date('Ymd/')),2));
	   //print_r(array_slice(scandir('public/img/'.date('Ymd/')),2));
	   $this->display();
	}
	//根据日期获取作品
	function huoquzuopin(){
	       $_SESSION['hdate']=$_POST['hdate'];
	       $this->ajaxReturn($_SESSION['hdate'],"","");
	 // $this->ajaxReturn($_POST['hdate'],"","");
	  }
    function texttoimg(){
	
	         $oldtext = $text = $_POST['word'];
             $haveBrLinker = trim($_POST['haveBrLinker']);
             $userStyle = explode('|', $_POST['userStyle']);
             $w= substr($_POST['w'],0,strlen($_POST['w'])-2);
	         $h=substr($_POST['h'],0,strlen($_POST['h'])-2);
	         $fsize=substr($_POST['fsize'],0,strlen($_POST['fsize'])-2);
             $ffamily=$_POST['ffamily'];
	         $fontdir=$_POST['fontdir'];
	         $fontFile ='public/fontlib/'.$fontdir.'/'.$ffamily.'.ttf';
		      if($text){
	              $text = substr($text, 0, 30000); #截取前一万个字符
	              $paddingTop = 20;
	              $paddingLeft = 15;
				  $paddingRight = 10;
	              $paddingBottom = 20;
	              $copyrightHeight = 36;
	              $canvasWidth = $w;
	              $canvasHeight = $paddingTop + $paddingBottom + $copyrightHeight;
	              $fontSize = $fsize;
	              $lineHeight = intval($fontSize * 1.3);
	              $textArr = array();
	              $tempArr = explode("\n", trim($text));
				  $j = 0;
				     foreach($tempArr as $v){
		              $arr = $this->str_div($v, intval(($w-$paddingLeft-$paddingRight)/($fsize*1.30)));
		              $textArr[] = array_shift($arr);
		         foreach($arr as $v){
			              $textArr[] = $haveBrLinker . $v;
			              $j ++;
			              if($j > 100){ break; }
		                    }
		                $j ++;
		               if($j > 100){ break; }
	                  }
	
	                 $textLen = count($textArr);
	                 $canvasHeight = $lineHeight * $textLen + $canvasHeight;
	                 $im = imagecreatetruecolor($canvasWidth, $canvasHeight); #定义画布
	                $colorArray = $this->str2rgb($userStyle[1]);
					imagefill($im, 0, 0, imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']));
	                $colorArray = $this->str2rgb('666666');
	                $colorLine = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);
	                 $padding = 3;
	                 $x1 = $y1 = $x4 = $y2 = $padding;
	                 $x2 = $x3 = $canvasWidth - $padding - 1;
	                 $y3 = $y4 = $canvasHeight - $padding - 1;
	                 imageline($im, $x1, $y1, $x2, $y2, $colorLine);
	                 imageline($im, $x2, $y2, $x3, $y3, $colorLine);
	                 imageline($im, $x3, $y3, $x4, $y4, $colorLine);
	                 imageline($im, $x4, $y4, $x1, $y1, $colorLine);
                                 //字体路径
	                  $fontStyle =$fontFile;
	                 if(!is_file($fontStyle)){
	                          $fontStyle='public/fontlib/qtzk/qgzt.ttf';
		
	}
	
	//写入四个随即数字
	$colorArray = $this->str2rgb($userStyle[0]);
	$fontColor = imagecolorallocate($im, $colorArray['red'], $colorArray['green'], $colorArray['blue']);
	
	foreach($textArr as $k=>$text){
		$offset = $paddingTop + $lineHeight * ($k + 1) - intval(($lineHeight-$fontSize) / 2);
		imagettftext($im, $fontSize, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);
	}
	
	$fontColor = imagecolorallocate($im, 0, 0, 0);
	$offset += 18;
	$text = '-----------------------------------------------------------------------';
	imagettftext($im, 10, 0, $paddingLeft, $offset, $fontColor, $fontStyle, $text);
	
	$offset += 18;
	$fontColor = imagecolorallocate($im, 255, 0, 0);
	$text = '作者:'.$this->user_info['nickname'];
	$fontStyle='public/fontlib/qtzk/qgzt.ttf';
	//iconv("GBK", "big5",
	imagettftext($im, 10, 0, $paddingLeft + 20, $offset, $fontColor, $fontStyle, $text);
	
	
	$imghost = 'http://'. $_SERVER['HTTP_HOST'] .__ROOT__.'/';
	$imgpath = 'public/img/' . date('Ymd/');
	if(!is_dir($imgpath)){ mkdir($imgpath); }
	$imgfile =  $imgpath . time() . rand(10000, 99999) . '.gif';
	$imgurl = $imghost . $imgfile;
	imagegif($im, $imgfile);
	imagedestroy($im);
	$url_title = urlencode(mb_substr($oldtext,0, 130, 'UTF-8'));
	$url_pic = urlencode($imgurl);
	
	 $this->ajaxReturn($imgfile ,"",0);         
}

		}
	function str_div($str, $width = 10){
	$strArr = array();
	$len = strlen($str);
	$count = 0;
	$flag = 0;
	while($flag < $len){
		if(ord($str[$flag]) > 128){
			$count += 1;
			$flag += 3;
		}
		else{
			$count += 0.5;
			$flag += 1 ;
		}
		if($count >= $width){
			$strArr[] = substr($str, 0, $flag);
			$str = substr($str, $flag);
			$len -= $flag;
			$count = 0;
			$flag = 0;
		}
	}
	$strArr[] = $str;
	return $strArr;
}
	     function str2rgb($str){
	         $color = array('red'=>0, 'green'=>0, 'blue'=>0);
	         $str = str_replace('#', '', $str);
	         $len = strlen($str);
	         if($len==6){
		            $arr=str_split($str,2);
		            $color['red'] = (int)base_convert($arr[0], 16, 10);
		            $color['green'] = (int)base_convert($arr[1], 16, 10);
		            $color['blue'] = (int)base_convert($arr[2], 16, 10);
		            return $color;
	                }
	         if($len==3){
		            $arr=str_split($str,1);
		            $color['red'] = (int)base_convert($arr[0].$arr[0], 16, 10);
		            $color['green'] = (int)base_convert($arr[1].$arr[1], 16, 10);
		            $color['blue'] = (int)base_convert($arr[2].$arr[2], 16, 10);
		      return $color;
	                    }
	return $color;
        }
   function getff() {
    //去掉跟随别的挤在一块的空白
        $ffdata = preg_replace('/\s(?=\s)/', '', trim(file_get_contents('public/ff/'.$_GET['id'].'.html')));

    //最后，去掉非space 的空白，用一个空格代替
        $ffdata = preg_replace('/[\n\r\t]/', ' ', $ffdata);
	    $this->ajaxReturn(json_decode($ffdata),"","");
   
   
   }

}